Skip to content

Demo: Seed charging and energy history - #36850

Closed
Maschga wants to merge 3 commits into
evcc-io:masterfrom
Maschga:feat/demo-history
Closed

Maschga wants to merge 3 commits into
evcc-io:masterfrom
Maschga:feat/demo-history

Conversation

@Maschga

@Maschga Maschga commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Seed sample history when evcc starts with --demo and neither charging sessions nor meter readings exist. This lets the app publisher’s screenshot workflow capture populated Sessions, Energy, and Battery views without waiting for the simulation to collect data.

  • Add sample charging sessions on most days over the past 40 days.
  • Add roughly a week of energy readings, including battery state of charge for the Battery chart.
  • Leave existing history untouched.

🤖 Generated with Hermes Agent

\cc @naltatis

@Maschga
Maschga marked this pull request as ready for review December 2, 2026 18:53

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="cmd/demo_history.go" line_range="15" />
<code_context>
+func seedDemoHistory(now time.Time) error {
+	return db.Instance.Transaction(func(tx *gorm.DB) error {
+		var count int64
+		if err := tx.Model(&session.Session{}).Count(&count).Error; err != nil || count != 0 {
+			return err
+		}
</code_context>
<issue_to_address>
**Concurrent demo starts fail seeding**

When two `--demo` instances seed the same database concurrently, both startup transactions pass the empty-history checks before either commits, then both attempt to create the same meter slots. The primary-key conflict (or a database write-lock error) aborts one transaction, and `runRoot` starts that instance in degraded mode instead of with demo history.

Serialize demo seeding across processes or handle a competing seed transaction by rechecking history and treating already-seeded data as success.

Also at `core/metrics/demo.go:14`.
</issue_to_address>

### Comment 2
<location path="core/metrics/demo.go" line_range="53" />
<code_context>
+		}
+		grid := home + charge + car - pv - discharge
+		for i, value := range [][2]float64{{pv, 0}, {home, 0}, {max(0, grid), max(0, -grid)}, {discharge, charge}, {car, 0}} {
+			history = append(history, meter{Meter: devices[i].Id, Timestamp: t.Unix(), Energy: value[0], ReturnEnergy: value[1], Recovered: slotMeasured})
+		}
+	}
</code_context>
<issue_to_address>
**Battery flows appear reversed**

When demo meter history is seeded and the energy view reads the battery rows, `SeedDemoHistory` stores `discharge` in `Energy` and `charge` in `ReturnEnergy`, but `sinkSlots` reads those fields as charge and discharge, respectively. The energy view therefore shows charging as battery output and discharging as battery input.

Store `charge` in `Energy` and `discharge` in `ReturnEnergy` for the battery rows.
</issue_to_address>

### Comment 3
<location path="core/metrics/demo.go" line_range="23" />
<code_context>
+		{Group: Home, Name: Home, Title: Home},
+		{Group: Grid, Name: "grid", Title: Grid},
+		{Group: Battery, Name: "battery", Title: "battery"},
+		{Group: Loadpoint, Name: "lp-1", Title: "Carport"},
+	}
+	for i := range devices {
</code_context>
<issue_to_address>
**Garage energy history is empty**

When a user views energy history for Garage after demo history is seeded, `SeedDemoHistory` creates and populates only the Carport loadpoint meter, while `seedDemoHistory` assigns alternating sessions to Garage; energy queries therefore return no Garage charging history despite the seeded Garage sessions.

Seed matching Garage loadpoint meter readings, or assign all synthetic sessions to Carport.

Also at `cmd/demo_history.go:48`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread cmd/demo_history.go Resolved
Comment thread core/metrics/demo.go Resolved
Comment thread core/metrics/demo.go Resolved
@Maschga Maschga added enhancement New feature or request infrastructure Basic functionality ux User experience/ interface labels Dec 2, 2026
@naltatis

naltatis commented Dec 3, 2026

Copy link
Copy Markdown
Member

Nice. It had that on my internal list for quite a while already. I'd even go a little further (more realistic data) and also add consumer and multiple pv's. We could also think about always using in memory sqlite when in --demo mode. No risk of interfering with real data.

@naltatis

naltatis commented Dec 3, 2026

Copy link
Copy Markdown
Member

I'll look into this and suggest an iteration.

@Maschga

Maschga commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator Author

Closing in favour of #36893

@Maschga Maschga closed this Dec 4, 2026
@Maschga
Maschga deleted the feat/demo-history branch December 4, 2026 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request infrastructure Basic functionality ux User experience/ interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants